/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author VANANH
 */
public class DownloadNews {
    private static String content = "";
    public static boolean downloadRssNews(){
        try {
            URL url = new URL("http://tapchilaptrinh.vn/feed/");
            URLConnection cnn = url.openConnection();
            InputStream is = cnn.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
           
            String line;
            while((line=reader.readLine())!=null){
                content+=line;
            }
            return true;
        } catch (Exception ex) {
            Logger.getLogger(DownloadNews.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    
    public static boolean saveNewsToXML(){
        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("tapchilaptrinh.xml")));
            bw.write(content);
            bw.flush();
            return true;
        } catch (Exception ex) {
            Logger.getLogger(DownloadNews.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
}
